﻿using System;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using System.IO;

namespace BneyBaruch.Ingest.FileSystem.Activities
{
    public partial class CheckFreeSpaceActivity : SequenceActivity
    {
        #region Fields

        private string _driveName;
        private int _minimumSpaceInMegabytes;

        #endregion

        #region Properties

        [Description("Drive name to check free space.")]
        public string DriveName
        {
            get
            {
                return _driveName;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new ArgumentNullException("DriveName");

                _driveName = value;
            }
        }

        [Description("Minimum space needed in given drive.")]
        public int MinimumSpaceInMegabytes
        {
            get
            {
                return _minimumSpaceInMegabytes;
            }
            set
            {
                _minimumSpaceInMegabytes = value;
            }
        }

        #endregion

        #region Constructors

        public CheckFreeSpaceActivity()
        {
            InitializeComponent();
        }

        #endregion

        #region Condition Methods

        private void IsEnoughSpace(object sender, ConditionalEventArgs e)
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.Name == DriveName && drive.IsReady)
                {
                    e.Result = MinimumSpaceInMegabytes >= drive.TotalFreeSpace / 1024 / 1024;
                    return;
                }
            }
            e.Result = true;
        }

        private void IsNotEnoughSpace(object sender, ConditionalEventArgs e)
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.Name == DriveName && drive.IsReady)
                {
                    e.Result = MinimumSpaceInMegabytes < drive.TotalFreeSpace / 1024 / 1024;
                    return;
                }
            }
            e.Result = true;
        }

        #endregion

        #region Code Activities ExecuteCode Methods

        private void enoughSpaceCodeActivity_ExecuteCode(object sender, EventArgs e)
        {

        }

        private void notEnoughSpaceCodeActivity_ExecuteCode(object sender, EventArgs e)
        {

        }

        #endregion
    }
}
